var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp()
Page({
  data: {
      index:0,
      currentpage:0,
  },
  onLoad(e) {
    this.setData({
      index: e.index
    })
  },
  onShow(e) {
    this.setData({
      phoneheight: app.globalData.phoneheight,
      fix_position: app.globalData.fix_position
    })
    var index = this.data.index;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    this.setData({
      group: prevPage.data.groups[index]
    });
    //设置lightsin,lightsout，需要从后端返回
    Ajax.AsyncPost("/group/group_info/", { groupid: app.globalData.activegroup.id }, (data) => {
      this.setData({
        lightsin: data.lightsin,
        lightsout: data.lightsout
      })
    });
  },
  bindchange: function (e) {
    this.setData({
      currentpage: e.detail.current
    })
  },
  checkcurrent: function (e) {
    var that = this;
    if (that.data.currentpage != e.target.dataset.current) {
      this.setData({currentpage: e.target.dataset.current})
    }
  },
  formSubmit: function(e){
    var name = e.detail.value.g_name;
    var index = this.data.index;
    Ajax.AsyncPost("/group/rename_group/", { groupid: app.globalData.activegroup.id, name: name}, (data) => {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      var temp_str = "groups[" + index + "].name"
      prevPage.setData({[temp_str]: name});
      this.setData({group: prevPage.data.groups[index]});
      wx.navigateBack();
    });
  },

  letin: function (e){
    var self = this;
    var index = e.currentTarget.dataset.index;
    var dst = self.data.lightsout[index].nodeaddress;
    var deviceKey = self.data.lightsout[index].devicekey;
    var groupAddress = app.globalData.activegroup.groupaddress;
    var blemesh = app.globalData.activemesh;
    Modal.showModal("确认将 " + self.data.lightsout[index].name + " 添加入组吗？",()=>{
      Modal.load("正在添加中...");
      blemesh.addGroup(groupAddress, dst, deviceKey, function (value) {
        if (value.code == 0) {
          Ajax.AsyncPost("/light/add_light_to_group/", { groupid: app.globalData.activegroup.id, lightid: self.data.lightsout[index].id }, (data) => {
            self.data.lightsin.push(self.data.lightsout[index]);
            self.data.lightsout.splice(index, 1);
            self.setData({
              lightsin: self.data.lightsin,
              lightsout: self.data.lightsout
            });
          });
          Modal.loadfinish();
        } else {
          Modal.showErr(value.message);
        }
      });
    })
  },

  letout: function (e){
    var self = this;
    var index = e.currentTarget.dataset.index;
    var dst = self.data.lightsin[index].nodeaddress;
    var deviceKey = self.data.lightsin[index].devicekey;
    var groupAddress = app.globalData.activegroup.groupaddress;
    var blemesh = app.globalData.activemesh;
    Modal.showModal("确认将 " + self.data.lightsin[index].name + " 删除出组吗？", () => {
      Modal.load("正在删除中中...");
      blemesh.removeGroup(groupAddress, dst, deviceKey, function (value) {
        console.log(value);
        if (value.code == 0) {
          Ajax.AsyncPost("/light/delete_light_of_group/", { groupid: app.globalData.activegroup.id, lightid: self.data.lightsin[index].id }, (data) => {
            self.data.lightsout.push(self.data.lightsin[index]);
            self.data.lightsin.splice(index, 1);
            self.setData({
              lightsin: self.data.lightsin,
              lightsout: self.data.lightsout
            });
          });
          Modal.loadfinish();
        } else {
          Modal.showErr(value.message);
        }
      });
    })
  },
})
